﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TZReport_Util.Util
{
    public class StringUtils
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content">待截取内容</param>
        /// <param name="start">开始字符串</param>
        /// <param name="end">结束字符串</param>
        /// <returns></returns>
        public static string GetConnectionString(string content, string start = "data source", string end = "multipleactiveresultsets")
        {
            int startIndex = content.IndexOf(start, StringComparison.InvariantCultureIgnoreCase);
            int endIndex = content.IndexOf(end, StringComparison.InvariantCultureIgnoreCase);
            content = content.Substring(startIndex, endIndex - startIndex);
            return content;
        }
        /// <summary>
        /// WeiXin_YuYue
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="wxyy"></param>
        /// <returns></returns>
        public static string ConvertObjToString<T>(T t, out string fieldNames) where T : class
        {
            string result = string.Empty;
            fieldNames = string.Empty;
            if (t == null)
                return result;

            List<string> listFieldName = new List<string>();
            PropertyInfo[] pros = t.GetType().GetProperties();
            List<string> listValue = new List<string>();
            foreach (PropertyInfo p in pros)
            {
                listFieldName.Add("[" + p.Name + "]");
                object objVal = p.GetValue(t);
                string oblValStr = string.Empty;
                if (objVal != null)
                    oblValStr = "'" + objVal.ToString() + "'";
                else
                    oblValStr = "'" + "'";

                listValue.Add(oblValStr);
            }
            fieldNames = string.Join(",", listFieldName);
            result = string.Join(",", listValue);
            return result;
        }

        public static string ReplaceEmpty(string content)
        {
            if (string.IsNullOrWhiteSpace(content))
                return content;

            content = Regex.Replace(content, @"\b\s+\b", string.Empty);
            return content;
        }
    }
}
